﻿// ********************************************
// ClassName  ：  {filename}
// Author        ：  
// Company     ： 智测心控
// Version        ：V1.0
// CreateTime ： {date}
// Description ：  
// ********************************************

using DAL.DB;
using DAL.Model;
using DataCenter;
using DcpServer.Common.Common;
using Newtonsoft.Json;
using System;
using System.Linq;

namespace WcfService.AlarmSettingService
{
    /// <summary>
    /// 报警设置服务实现类
    /// </summary>
    public class AlarmSettingService : IAlarmSettingService
    {
        /// <summary>
        /// 添加报警设置
        /// </summary>
        /// <param name="str">str</param>
        /// <returns>报警设置</returns>
        public string AlarmSettingAdd(string str)
        {
            using (var db = new YFrameContext())
            {
                AlarmSetting alarmSettingModel = JsonConvert.DeserializeObject<AlarmSetting>(str);
                alarmSettingModel.Id = Guid.NewGuid();
                alarmSettingModel.CreateTime = DateTime.Now;
                alarmSettingModel.Creator = "admin";
                db.AlarmSetting.Add(alarmSettingModel);

                var result = db.SaveChanges();

                if (result > 0)
                {
                    DcpContext.Instance.AlarmSetting = alarmSettingModel;
                }

                var json = new
                {
                    isTrue = result >= 0,
                    msg = result >= 0 ? "添加成功" : "添加失败",
                };
                return JsonConvert.SerializeObject(json);
            }
        }

        /// <summary>
        /// 报警设置查询
        /// </summary>
        /// <returns>报警设置</returns>
        public AlarmSetting GetAlarmSettingObject()
        {
            using (var db = new YFrameContext())
            {
                string macAddress = CommonHelper.GetLocalMAC();
                var patid = Guid.Empty;
                if (DcpContext.Instance.CurentPatient != null)
                {
                    patid = DcpContext.Instance.CurentPatient.Id;
                }

                var alarmSettingInfo = db.AlarmSetting.Where(u => u.LocalMacAddress == macAddress && u.PatientId == patid).FirstOrDefault();

                return alarmSettingInfo;
            }
        }

        /// <summary>
        /// 报警设置查询
        /// </summary>
        /// <param name="str">str</param>
        /// <returns>报警设置</returns>
        public string AlarmSettingSelect()
        {
            return JsonConvert.SerializeObject(GetAlarmSettingObject());
        }

        /// <summary>
        /// 修改报警设置
        /// </summary>
        /// <param name="str">str</param>
        /// <returns>报警设置</returns>
        public string AlarmSettingUpdate(string str)
        {
            using (var db = new YFrameContext())
            {
                AlarmSetting alarmInfo = JsonConvert.DeserializeObject<AlarmSetting>(str);
                var systemSettingInfo = db.AlarmSetting.Where(u => u.LocalMacAddress == alarmInfo.LocalMacAddress && u.PatientId == alarmInfo.PatientId).FirstOrDefault();
                systemSettingInfo.AlarmPauseTime = alarmInfo.AlarmPauseTime;
                systemSettingInfo.IsEcgAlarmOn = alarmInfo.IsEcgAlarmOn;
                systemSettingInfo.EcgAlarmLevel = alarmInfo.EcgAlarmLevel;
                systemSettingInfo.EcgAlarmUpperLimit = alarmInfo.EcgAlarmUpperLimit;
                systemSettingInfo.EcgAlarmLowestLimit = alarmInfo.EcgAlarmLowestLimit;
                systemSettingInfo.IsNibpAlarmOn = alarmInfo.IsNibpAlarmOn;
                systemSettingInfo.NibpAlarmLevel = alarmInfo.NibpAlarmLevel;
                systemSettingInfo.NsAlarmUpperLimit = alarmInfo.NsAlarmUpperLimit;
                systemSettingInfo.NsAlarmLowestLimit = alarmInfo.NsAlarmLowestLimit;
                systemSettingInfo.NmAlarmUpperLimit = alarmInfo.NmAlarmUpperLimit;
                systemSettingInfo.NmAlarmLowestLimit = alarmInfo.NmAlarmLowestLimit;
                systemSettingInfo.NdAlarmUpperLimit = alarmInfo.NdAlarmUpperLimit;
                systemSettingInfo.NdAlarmLowestLimit = alarmInfo.NdAlarmLowestLimit;
                systemSettingInfo.IsSpo2AlarmOn = alarmInfo.IsSpo2AlarmOn;

                systemSettingInfo.Spo2AlarmLevel = alarmInfo.Spo2AlarmLevel;
                systemSettingInfo.Spo2AlarmUpperLimit = alarmInfo.Spo2AlarmUpperLimit;
                systemSettingInfo.Spo2AlarmLowestLimit = alarmInfo.Spo2AlarmLowestLimit;
                systemSettingInfo.IsRespAlarmOn = alarmInfo.IsRespAlarmOn;
                systemSettingInfo.RespAlarmLevel = alarmInfo.RespAlarmLevel;
                systemSettingInfo.RespAlarmUpperLimit = alarmInfo.RespAlarmUpperLimit;
                systemSettingInfo.RespAlarmLowestLimit = alarmInfo.RespAlarmLowestLimit;
                systemSettingInfo.AsphyxiaAlarmTime = alarmInfo.AsphyxiaAlarmTime;
                systemSettingInfo.IsTempAlarmOn = alarmInfo.IsTempAlarmOn;
                systemSettingInfo.TempAlarmLevel = alarmInfo.TempAlarmLevel;
                systemSettingInfo.TempAlarmUpperLimit = alarmInfo.TempAlarmUpperLimit;
                systemSettingInfo.TempAlarmLowestLimit = alarmInfo.TempAlarmLowestLimit;

                var result = db.SaveChanges();

                if (result > 0)
                {
                    DcpContext.Instance.AlarmSetting = systemSettingInfo;
                }

                var json = new
                {
                    isTrue = result >= 0,
                    msg = result >= 0 ? "修改成功" : "修改失败",
                };
                return JsonConvert.SerializeObject(json);
            }
        }
    }
}
